package com.onccc.social.step

import android.view.accessibility.AccessibilityNodeInfo
import com.blankj.utilcode.util.AppUtils
import com.onccc.social.Assists
import com.onccc.social.Assists.click
import com.onccc.social.Assists.getBoundsInScreen
import com.onccc.social.Assists.getChildren
import com.onccc.social.Assists.paste
import com.onccc.social.MainActivity
import com.onccc.social.MainActivity.Companion.getInputTime
import com.onccc.social.MainActivity.Companion.getOneTime
import com.onccc.social.MainActivity.Companion.getThreeTime
import com.onccc.social.MainActivity.Companion.getTwoTime
import com.onccc.social.OverManager
import com.onccc.social.model.CopyWriteModel
import com.onccc.social.stepper.Step
import com.onccc.social.stepper.StepCollector
import com.onccc.social.stepper.StepImpl
import com.onccc.social.stepper.StepManager
import kotlinx.coroutines.delay


/**
 * @author GongChengHao
 * @date 2024/12/4
 * Description :
 */
class InstagramCreateSocial : StepImpl() {

    var mPackageName = "com.instagram.android"

    private var writeModel = CopyWriteModel()

    override fun onImpl(collector: StepCollector) {
        collector.next(StepTag.STEP_1) {
            OverManager.log("启动instagram")

            AppUtils.launchApp(mPackageName)
            runIO { delay(getTwoTime()) }

            Assists.findByText("Accept").forEach() {
                runIO { delay(1000) }
                it.click()
            }
            return@next Step.get(StepTag.STEP_2)
        }.next(StepTag.STEP_2) {
            Assists.findById("com.instagram.android:id/wrapper").forEach {it1->
                Assists.findById("com.instagram.android:id/creation_tab").forEach {
                    runIO { delay(getOneTime()) }
                    val screen = it.getBoundsInScreen()
                    if (screen.top > 1750) {
                        OverManager.log("点击创建")
                        Assists.gestureViewClick(it)
                        return@next Step.get(StepTag.STEP_3)
                    }
                }
            }

            if (Assists.getPackageName() == mPackageName) {
                runIO { delay(getOneTime()) }
                Assists.back()
                return@next Step.get(StepTag.STEP_2)
            }
            return@next Step.get(StepTag.STEP_1)
        }.next(StepTag.STEP_3) {
            runIO { delay(getOneTime()) }
            Assists.findById("com.instagram.android:id/gallery_folder_menu_tv").firstOrNull() {
                runIO { delay(getTwoTime()) }
                OverManager.log("进入相册添加")
                Assists.gestureViewClick(it)
                return@next Step.get(StepTag.STEP_4)
            }

            return@next Step.get(StepTag.STEP_2)
        }.next(StepTag.STEP_4) {
            runIO { delay(getTwoTime()) }
            Assists.findById("com.instagram.android:id/filter_type_recyclerview").forEach {
                runIO { delay(getOneTime()) }
                OverManager.log("切换到视频选择")
                Assists.gestureViewClick(it.getChild(2))
                return@next Step.get(StepTag.STEP_5)
            }
            return@next Step.get(StepTag.STEP_100)
        }.next(StepTag.STEP_5) {
            runIO { delay(getOneTime()) }
            Assists.findById("com.instagram.android:id/camera_settings_gear").forEach {
                runIO { delay(getTwoTime()) }
                OverManager.log("选择视频下一步")
                Assists.gestureViewClick(it)
                return@next Step.get(StepTag.STEP_6)
            }

            return@next Step.get(StepTag.STEP_2)
        }.next(StepTag.STEP_6) {
            runIO { delay(getOneTime()) }
            Assists.findById("com.instagram.android:id/bb_primary_action_container").forEach {
                runIO { delay(getTwoTime()) }
                OverManager.log("关闭弹窗")
                Assists.gestureViewClick(it)
            }

            runIO { delay(getOneTime()) }
            Assists.findById("com.instagram.android:id/auxiliary_button").forEach {
                runIO { delay(getTwoTime()) }
                OverManager.log("关闭弹窗")
                Assists.gestureViewClick(it)
            }

            runIO { delay(getThreeTime()) }
            Assists.findById("com.instagram.android:id/clips_right_action_button").forEach {
                runIO { delay(getTwoTime()) }
                OverManager.log("视频选择完成")
                Assists.gestureViewClick(it)
                return@next Step.get(StepTag.STEP_9)
            }
            return@next Step.get(StepTag.STEP_100)
        }.next(StepTag.STEP_9) {
            runIO { delay(getOneTime()) }
            Assists.findById("com.instagram.android:id/clips_download_privacy_nux_button").forEach {
                runIO { delay(getTwoTime()) }
                OverManager.log("关闭弹窗")
                Assists.gestureViewClick(it)
            }

            runIO { delay(getTwoTime()) }
            MainActivity.getPostContent("instagram", { model ->
                writeModel = model
                StepManager.execute(InstagramCreateSocial::class.java, StepTag.STEP_10, begin = true)
            })
            {
                StepManager.execute(InstagramCreateSocial::class.java, StepTag.STEP_10, begin = true)

            }
            return@next Step.none
        }.next(StepTag.STEP_10) {
            runIO { delay(getOneTime()) }
            OverManager.log("点击输入框")
            Assists.findById("com.instagram.android:id/caption_input_text_view").forEach {
//               val editNode =  it.getChild(0).getChild(0).getChild(1).getChild(0).getChild(0)
                runIO { delay(getTwoTime()) }
                Assists.gestureViewClick(it)

                runIO { delay(getOneTime()) }
                it.performAction(AccessibilityNodeInfo.ACTION_FOCUS)
                for (s in writeModel.copywriting) {
                    it.paste(s.toString())
                    runIO { delay(getInputTime()) }
                }
                if (writeModel.hashtags.size == 0) {
                    return@next Step.get(StepTag.STEP_12)
                }
                var hashtag = ""
                for (s in writeModel.hashtags) {
                    hashtag += "#$s "
                }
                for (hs in hashtag) {
                    it.paste(hs.toString())
                    runIO { delay(getInputTime()) }
                }

                runIO { delay(getOneTime()) }
                Assists.back()

                return@next Step.get(StepTag.STEP_12)
            }
            return@next Step.get(StepTag.STEP_100)
        }.next(StepTag.STEP_11) {
            runIO { delay(getOneTime()) }

            return@next Step.get(StepTag.STEP_100)
        }.next(StepTag.STEP_12) {
            runIO { delay(getTwoTime()) }
            Assists.findById("com.instagram.android:id/share_button").firstOrNull() {
                OverManager.log("确定发送")
                runIO { delay(getThreeTime()) }
                Assists.gestureViewClick(it)
                return@next Step.get(StepTag.STEP_100)
            }
            return@next Step.get(StepTag.STEP_12)
        }.next(StepTag.STEP_97) {
            runIO { delay(getOneTime()) }
            Assists.back()
            return@next Step.get(StepTag.STEP_98)
        }.next(StepTag.STEP_100) {
            OverManager.log("结束")
            return@next Step.none
        }
    }
}